package com.ellfors.module_first.multi_list_type

import com.ellfors.common.base.adapter.BaseMultiAdapter
import com.ellfors.module_first.databinding.ItemMultiFirstBinding
import com.ellfors.module_first.databinding.ItemMultiSecondBinding
import com.ellfors.module_first.databinding.ItemMultiThirdBinding

class MultiListTypeAdapter : BaseMultiAdapter<MultiBean>() {

    companion object {
        const val ITEM_TYPE_1 = 1
        const val ITEM_TYPE_2 = 2
        const val ITEM_TYPE_3 = 3
    }

    override fun registerItemTypes(): List<Int> {
        return listOf(ITEM_TYPE_1, ITEM_TYPE_2, ITEM_TYPE_3)
    }

    override fun createViewHolder(itemType: Int) {
        when (itemType) {
            ITEM_TYPE_1 -> createType1()
            ITEM_TYPE_2 -> createType2()
            ITEM_TYPE_3 -> createType3()
        }
    }

    override fun dispatchItemType(data: MultiBean, position: Int): Int {
        return data.type
    }

    private fun createType1() {
        ITEM_TYPE_1.registerItem(
            createBinding = ItemMultiFirstBinding::inflate,
            convert = { holder, _, item ->
                holder.binding.tvText.text = item?.data ?: ""
            })
    }

    private fun createType2() {
        ITEM_TYPE_2.registerItem(
            createBinding = ItemMultiSecondBinding::inflate,
            convert = { holder, _, item ->
                holder.binding.tvText.text = item?.data ?: ""
            })
    }

    private fun createType3() {
        ITEM_TYPE_3.registerItem(
            createBinding = ItemMultiThirdBinding::inflate, convert = { holder, _, item ->
                holder.binding.tvText.text = item?.data ?: ""
            }, isFullSpanItem = true
        )
    }
}